//
//  ModalLoading.m
//  飞波
//
//  Created by 开发 on 11-8-5.
//  Copyright 2011 Feibo Inc. All rights reserved.
//

#import "ModalLoading.h"

#define LOADINGVIEWTAG	442

@implementation UIViewController (ModalLoading)

- (void)setObjectsEnabled:(BOOL)en
{
	NSMutableArray * objs = [NSMutableArray array];
	if (self.view) {
		[objs addObject:self.view];
	}
	if (self.navigationController) {
		[objs addObject:self.navigationController.navigationBar];
	}
	if (objs) {
		for (id item in objs) {
			if (![item retainCount]) {
				continue;
			}
			if ([item respondsToSelector:@selector(setUserInteractionEnabled:)]) {
				[item setUserInteractionEnabled:en];
			}
			if ([item respondsToSelector:@selector(setEnabled:)]) {
				[item setEnabled:en];
			}
		}
	}
}

- (void)addLoadingLabel:(FBLoadingSuperViewType)sViewType isAnimate:(BOOL)ia labelText:(NSString *)text
{
	UIView * superView = sViewType==FBLoadingSuperViewTypeSelfView?self.view:self.navigationController.view;
	[self setObjectsEnabled:NO];
	
	UIImage *btnBGImage = [[UIImage imageNamed:@"loading_bg.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
	
	UIScreen * screen = [UIScreen mainScreen];
	
	CGFloat screenWidth = screen.bounds.size.width;
	CGFloat screenHeight = screen.bounds.size.height;
	
	CGRect btnRect = CGRectMake(0, 0, screenWidth, screenHeight);
	
	UIButton *loadingButon = [[UIButton alloc] initWithFrame:btnRect];
	[loadingButon setTitle:text forState:UIControlStateNormal];	
	[loadingButon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[loadingButon.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
	[loadingButon setTitleEdgeInsets:UIEdgeInsetsMake(95, -50, 10, 100)];
	loadingButon.titleLabel.numberOfLines = 2;
	[loadingButon.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
	loadingButon.titleLabel.textAlignment = UITextAlignmentCenter;
	
	[loadingButon setImage:btnBGImage forState:UIControlStateNormal];
	
	CGFloat svWidth = superView.bounds.size.width;
	CGFloat svHeight = superView.bounds.size.height;
	
	[loadingButon setImageEdgeInsets:UIEdgeInsetsMake(svHeight/2 - 77, svWidth/2 - 77, svHeight/2 - 77, svWidth/2 - 77)];
	
	[loadingButon setBackgroundColor: [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.4f]];
	[loadingButon setUserInteractionEnabled:NO];
	[loadingButon setTag:LOADINGVIEWTAG];
	
	[superView addSubview:loadingButon];

	[loadingButon release];
	
	[self setLoadingImage:sViewType andType:FBModleLoadingIconIndicator];
}

- (void)removeLoadingLabelFrom:(FBLoadingSuperViewType)sViewType withAnimate:(BOOL)wa andText:(NSString *)text resultType:(BOOL)rt
{
	UIView * superView = sViewType==FBLoadingSuperViewTypeSelfView?self.view:self.navigationController.view;
	
	UIButton * loadingButton = (UIButton *)[superView viewWithTag:LOADINGVIEWTAG];
	
	if (!loadingButton) {
		[self setObjectsEnabled:YES];
	}
	
	if (rt) {
		[self setLoadingImage:sViewType andType:FBModleLoadingIconDone];
	} else {
		[self setLoadingImage:sViewType andType:FBModleLoadingIconWaring];
	}

	if (wa) {
		[loadingButton setTitle:text forState:UIControlStateNormal];
		[UIView animateWithDuration:1.2f
						 animations:^{loadingButton.alpha = 0.0;}
						 completion:^(BOOL finished){ 
							 [loadingButton removeFromSuperview];
							 [self setObjectsEnabled:YES];
						 }];
	} else {
		[loadingButton removeFromSuperview];
		[self setObjectsEnabled:YES];
	}
}

- (void)setLoadingLabelTextIn:(FBLoadingSuperViewType)sViewType andText:(NSString *)text
{
	UIView * superView = sViewType==FBLoadingSuperViewTypeSelfView?self.view:self.navigationController.view;
	UIButton * loadingButton = (UIButton *)[superView viewWithTag:LOADINGVIEWTAG];
	if (loadingButton) [loadingButton setTitle:text forState:UIControlStateNormal];
}

- (void)setLoadingImage:(FBLoadingSuperViewType)sViewType andType:(FBModleLoadingIcon)iconType
{
	UIView * superView = sViewType==FBLoadingSuperViewTypeSelfView?self.view:self.navigationController.view;
	UIButton * loadingButton = (UIButton *)[superView viewWithTag:LOADINGVIEWTAG];
	[[loadingButton viewWithTag:LOADINGVIEWTAG * 2] removeFromSuperview];
	[[loadingButton viewWithTag:LOADINGVIEWTAG * 2 + 1] removeFromSuperview];
	[[loadingButton viewWithTag:LOADINGVIEWTAG * 2 + 2] removeFromSuperview];
	if (iconType == FBModleLoadingIconIndicator) {
		UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
		indicator.frame = CGRectMake((loadingButton.bounds.size.width - 22)/2, (loadingButton.bounds.size.height - 32)/2,
									 22, 22);
		indicator.tag = LOADINGVIEWTAG * 2;
		indicator.hidesWhenStopped = YES;
		[indicator startAnimating];
		[loadingButton addSubview:indicator];
		[indicator release];
	} else if (iconType == FBModleLoadingIconWaring) {
		UIImageView * doneImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_warning.png"]];

		doneImage.frame = CGRectMake((loadingButton.bounds.size.width - 42)/2, (loadingButton.bounds.size.height - 42)/2,
									 44, 44);
		doneImage.tag = LOADINGVIEWTAG * 2 + 1;
		[loadingButton addSubview:doneImage];
		[doneImage release];
	} else if (iconType == FBModleLoadingIconDone) {
		UIImageView * doneImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_ok.png"]];
		
		doneImage.frame = CGRectMake((loadingButton.bounds.size.width - 42)/2, (loadingButton.bounds.size.height - 42)/2,
									 44, 44);
		doneImage.tag = LOADINGVIEWTAG * 2 + 2;
		[loadingButton addSubview:doneImage];
		[doneImage release];
	}
}

@end
